#pragma once

#include <zenoui/check_gl.hpp>
#include <zenox/minilog.hpp>

namespace zenoui {

struct FPSCounter {
private:
    double t0 = glfwGetTime();
    int counter = 0;
    const int interval = 24;
    double non_duty_t0 = 0.0;
    double non_duty_dt = 0.0;
public:
    double fps = 0.0, duty_ratio = 0.0;

    void non_duty_begin() {
        non_duty_t0 = glfwGetTime();
    }

    void non_duty_end() {
        non_duty_dt += glfwGetTime() - non_duty_t0;
    }

    bool update_counter() {
        if (!(counter = (counter + 1) % interval)) {
            double t1 = glfwGetTime();
            double dt = t1 - t0;
            fps = (double)interval / dt;
            duty_ratio = 1.0 - non_duty_dt / dt;
            duty_ratio *= 100.0;
            non_duty_dt = 0.0;
            zenox::log_trace("fps={:.02f}, duty_ratio={:.01f}%", fps, duty_ratio);
            t0 = t1;
            return true;
        }
        return false;
    }
};

}
